package bingosoft.metro.datasync.service;

import bingosoft.components.base.service.*;
import bingosoft.metro.datasync.base.form.SyncLogForm;
import bingosoft.metro.datasync.base.model.SyncLogModel;
import bingosoft.metro.datasync.iservice.IDatasyncService;
import bingosoft.metro.datasync.uuap.service.OrgSyncService;
import bingosoft.metro.datasync.uuap.service.UserSyncService;
import leap.core.annotation.Bean;
import leap.core.annotation.Inject;
import leap.core.annotation.M;
import leap.lang.Strings;
import leap.orm.query.PageResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenwx
 * @description 数据同步业务服务类
 * @createTime 2019-11-25 10:19
 */
@Bean
public class DatasyncService extends BaseService implements IDatasyncService {

    @Inject @M
    OrgSyncService orgSyncService;

    @Inject @M
    UserSyncService userSyncService;

    /**
     * 执行组织全量同步
     *
     * @return 同步结果
     */
    @Override
    public Result execOrgFullSync() {
        this.logMethodCalled("execOrgFullSync");

        //设置全量同步标识
        orgSyncService.setFullSync(true);
        //执行同步
        Result result = orgSyncService.syncData();

        return result;
    }

    /**
     * 执行组织增量同步
     *
     * @return 同步结果
     */
    @Override
    public Result execOrgIncSync() {
        this.logMethodCalled("execOrgIncSync");

        //执行同步
        Result result = orgSyncService.syncData();

        return result;
    }

    /**
     * 执行用户全量同步
     *
     * @return 同步结果
     */
    @Override
    public Result execUserFullSync() {
        this.logMethodCalled("execUserFullSync");

        //设置全量同步标识
        userSyncService.setFullSync(true);
        //执行同步
        Result result = userSyncService.syncData();

        return result;
    }

    /**
     * 执行用户增量同步
     *
     * @return 同步结果
     */
    @Override
    public Result execUserIncSync() {
        this.logMethodCalled("execUserIncSync");

        //执行同步
        Result result = userSyncService.syncData();

        return result;
    }

    /**
     * 分页查询同步日志
     *
     * @param pageQueryRequest
     * @param params
     * @return
     */
    @Override
    public ResultEntity<ResultPage<List<SyncLogForm>>> pageQuerySyncLog(PageQueryRequest pageQueryRequest, Map<String, Object> params) {
        this.logMethodCalled("pageQuerySyncLog");
        this.logParamValue("pageQueryRequest", pageQueryRequest);
        this.logParamValue("params", params);

        ResultEntity<ResultPage<List<SyncLogForm>>> resultEntity = new ResultEntity<>();

        if (pageQueryRequest ==  null){
            pageQueryRequest = new PageQueryRequest();
        }
        if(params == null){
            params = new HashMap<>();
        }

        int pageIndex = pageQueryRequest.getIndex();
        int pageSize = pageQueryRequest.getSize();
        String sort = pageQueryRequest.getSort();
        if(Strings.isBlank(sort)){
            sort = "beginTime desc";
        }

        //构建查询语句
        String sql = this.buildSelectSql("datasync-queryDataSyncLog");
        pageQueryRequest.mergeFilters(sql, params);

        try{
            //执行分页查询
            PageResult<SyncLogModel> pageResult = SyncLogModel.<SyncLogModel>query(pageQueryRequest.getMergeSql())
                    .params(pageQueryRequest.getParameter())
                    .orderBy(sort)
                    .pageResult(pageIndex, pageSize);

            //转换为表单对象
            List<SyncLogForm> syncLogForms = new ArrayList<>();
            for(SyncLogModel syncLogModel: pageResult.list()){
                SyncLogForm syncLogForm = new SyncLogForm();
                syncLogForm.copyFrom(syncLogModel);
                syncLogForms.add(syncLogForm);
            }

            //设置分页查询结果
            ResultPage<List<SyncLogForm>> resultPage = new ResultPage();
            resultPage.setPage(pageIndex);
            resultPage.setSize(pageSize);
            resultPage.setRecords(pageResult.getTotalCount());
            resultPage.setRows(syncLogForms);

            resultEntity.setBusinessObject(resultPage);
        }catch (Exception e){
            this.logError("数据同步日志查询失败", e);
            resultEntity.setSuccess(false);
            resultEntity.setMessage(Strings.format("数据同步日志查询失败【{0}】", e.getMessage()));
        }

        return resultEntity;
    }
}
